package com.twu.biblioteca.controller;

import com.twu.biblioteca.model.Item;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by madhu on 1/5/14.
 */
public class ItemController {

    public ItemController() {

    }

    public List<Item> getItemsInStack(List<Item> items) {

        List<Item> itemsInStack = new ArrayList<Item>();
        Iterator iterator = items.iterator();
        while(iterator.hasNext()) {
            Item item = (Item) iterator.next();
            if(item.isInStack())
                itemsInStack.add(item);
        }
        return itemsInStack;

    }

    public boolean checkoutItem(String name, List<Item> items) {

        Item item = getItemByName(name, items);
        if(!isItemNull(item) && item.isInStack()) {
            item.setInStack(false);
            return true;
        }
        return false;
    }

    public boolean returnItem(String name, List<Item> items) {

        Item item = getItemByName(name, items);
        if(!isItemNull(item) && !item.isInStack()) {
            item.setInStack(true);
            return true;
        }
        return false;

    }

    public Item getItemByName(String name, List<Item> items) {

        Iterator iterator = items.iterator();
        while(iterator.hasNext()) {
            Item item = (Item) iterator.next();
            if((name.toUpperCase()).equals(item.getName().toUpperCase()))
                return item;
        }
        return null;

    }

    public boolean isItemNull(Item item) {
        if(item==null) return true;
        return false;
    }

}
